#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;


using System.Web;
using WaveMaster.Holidays.Club.Service;
using WaveMaster.Holidays.Club.Domain;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class UserProfileForm : Form
    {
        User m_current_user;
        UserService m_userService;


        public UserProfileForm(User user)
        {
            InitializeComponent();
            m_userService = new UserService();// (UserService)ServiceFactory.Instance().GetService(ServiceFactory.USERSERVICE);
            m_current_user = user;
            labelEmailID.Text = user.EmailID;
            labelExpireDate.Text = user.ExpiryDate.ToString("dd-MMM-yyyy");
            labelUserCode.Text = user.UserCode;
            labelUserName.Text = user.UserName;
            labelTravelAgent.Text = user.TravelAgent.Name; ;
            LoadPrinterOptions();
        }

        private void LoadPrinterOptions()
        {
            checkBoxBP.Checked = m_current_user.DirectPrintBP;
            checkBoxKIMS.Checked = m_current_user.DirectPrintKIMS;
            checkBoxWC.Checked = m_current_user.DirectPrintWC;
            textBoxMACAddress.Text = m_current_user.MACAddress;
            textBoxWCPrinterName.Text = m_current_user.PrinterNameWC;
            textBoxBPPrintername.Text = m_current_user.PrinterNameBP;
            textBoxKIMSPrinterName.Text = m_current_user.PrinterNameKIMS; 
        }

        private void buttonChangePassword_Click(object sender, EventArgs e)
        {
            try
            {
                if (isValid())
                {
                    m_current_user.ModificationBy = m_current_user.UserID;
                    m_current_user.ModificationTerminal = HttpContext.Current.User.Identity.Name.ToString() + "," + HttpContext.Current.Request.UserHostAddress;
                    m_userService.ChangePassword(m_current_user, textBoxNewPassword.Text);
                    m_current_user.Password = textBoxNewPassword.Text;
                    MessageBox.Show("Password successfuly changed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    clearForm();
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
        }

        private void clearForm()
        {
            textBoxPassword.Text = string.Empty;
            textBoxNewPassword.Text = string.Empty;
            textBoxConfirmPassword.Text = string.Empty;
        }

        private bool isValid()
        {
            bool valid_a = m_current_user.Password == textBoxPassword.Text;
            bool valid_b = textBoxNewPassword.Text != string.Empty;
            bool valid_c = textBoxNewPassword.Text == textBoxConfirmPassword.Text;
            if (!valid_a)
                throw new Exception("Current Password is wrong");
            if (!valid_b)
                throw new Exception("New Password can be empty");
            if(!valid_c)
                throw new Exception("Confirmation Password is different");
            return valid_a && valid_b && valid_c;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UserProfileForm_Load(object sender, EventArgs e)
        {
            textBoxPassword.SelectAll();
        }

        private void checkBoxWC_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBoxBP_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void buttonUpdatePrinterOptions_Click(object sender, EventArgs e)
        {
            try
            {
                m_current_user.PrinterNameBP = textBoxBPPrintername.Text;
                m_current_user.PrinterNameWC = textBoxWCPrinterName.Text;
                m_current_user.PrinterNameKIMS = textBoxKIMSPrinterName.Text;
                m_current_user.DirectPrintBP = checkBoxBP.Checked;
                m_current_user.DirectPrintWC = checkBoxWC.Checked;
                m_current_user.DirectPrintKIMS = checkBoxKIMS.Checked;

                m_current_user.MACAddress = textBoxMACAddress.Text.ToUpper();
                m_userService.UpdateUserPrinterOptions(m_current_user);
                MessageBox.Show("Printer Options successfuly changed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
        }
    }
}